#-*-coding:utf-8-*-
from fileoperate import FileOperate
from test import test
'''
自定义的压缩方法
'''
class MatrixCompress:
    '''
    自我设计的矩阵压缩方法,至于具体的表现形式有待完善
    '''
    def __init__(self,lent = 16,width = 16):
        '''
        lent为高度,width为宽度
        '''
        self.content = [[0 for col in range(width)]for row in range(lent)]  #建立矩阵
        self.fileModule = FileOperate()         #申请文件操作对象,主要用来转换数据
        self.col = width
        self.row = lent

    def AddByte(self,In,loc):
        '''
        将数据加入矩阵
        '''
        pos = self.fileModule.ByteToDigital(In)
        if self.content[pos[0]][pos[1]] == 0:
            self.content[pos[0]][pos[1]] = [loc]        #是第一个元素,更改相应位置
        else:
            self.content[pos[0]][pos[1]].append(loc)    #不是第一个元素附加

    def MatrixDataByPos(self,col,row):
        '''
        返回相应位置的数据
        '''
        if self.content:
            return self.content[col][row]
        return None

    def GetDataLength(self):
        '''
        得到数据长度
        '''
        ltn = 0
        if self.content:
            for i in self.col:
                for j in self.row:
                    if self.content[i][j] != 0:
                        ltn += len(self.content[i][j])
        else:
            return None     #没有完成初始化

    def GetDataStr(self):
        '''
        得到字符串数据流
        '''
        outstr = ''
        if self.content:
            for i in range(self.row):
                for j in range(self.col):
                    if self.content[i][j] == 0:
                        outstr += ','
                    else:
                        l = len(self.content[i][j])
                        for k in range(l):
                            outstr += (str(self.content[i][j][k]))
                            if k != (l-1):
                                outstr += ' '
                            else:
                                outstr += ','
                outstr += '\n'
            return outstr
        else:
            return None

    def ConstructMartixByStr(self,strData):
        '''
        由字符串构建数据
        '''
        col = 0
        row = 0
        ntotal = 0
        slent = len(strData)
        while ntotal < slent:
            #当已处理数据跟数据一样长的时候结束循环
            flag = strData[ntotal] #to save the convert in order to save calc time
            if (flag == ','):           #judge the ','
                col += 1
                if col == self.col:
                    col =0
                flag1 = strData[ntotal +1]
                if (flag1 == ','):   #the situation ',,'.This means the content[row][col] = 0
                    ntotal += 1
                    continue
                elif flag1 == '\n': #the situation ',\n'.This means row += 1,col = 0
                    row += 1
                    ntotal += 2
                else:
                    i = 1
                    while strData[ntotal + 1 + i] != ',':
                        i += 1
                    ktemp = strData[ntotal + 1 : ntotal + 1 + i]
                    slist = ktemp.split(' ')        #分解链表
                    for j in range(len(slist)):
                        slist[j] = int(slist[j])
                    self.content[row][col] = slist
                    ntotal = ntotal + 1 + i     #jump
            else:
                i = 0
                while strData[ntotal + 1 + i] != ',':
                    i += 1
                ktemp = strData[ntotal:ntotal + 1 + i]
                slist = ktemp.split(' ')
                for j in range(len(slist)):
                    slist[j] = int(slist[j])
                self.content[row][col] = slist
                ntotal = ntotal + 1 + i

    def test(self,row = 16,col = 16,xmin = 100,xmax = 1000):
        '''
        this function is used to test the modules
        '''
        k = test()
        f = FileOperate()
        self.content = k.GetTestMatrixData(row,col,xmin,xmax)
        print self.content
        f.WriterFileStr('/home/hucheng/workspace/graduateproject/src/modules/content.txt',str(self.content))

        dstr = self.GetDataStr()
        print dstr
        f.WriterFileStr('/home/hucheng/workspace/graduateproject/src/modules/datastr.txt',dstr)

        self.__init__()             #clear the momery
        self.ConstructMartixByStr(dstr)
        print self.content
        f.WriterFileStr('/home/hucheng/workspace/graduateproject/src/modules/recontent.txt',str(self.content))

if __name__ == '__main__':
    k = MatrixCompress()
    k.test()

